﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.common;
using rock.BLL;

namespace rock.UI.houseinfo
{
    public partial class UserControlHouseDong : UserControl
    {
        DalHOUSE_DONG dal = new DalHOUSE_DONG();
        public UserControlHouseDong()
        {
            InitializeComponent();
            this.userControlCommonListHeader1.addClick = addClick;
            this.userControlCommonListHeader1.updateClick = updateClick;
            this.userControlCommonListHeader1.deleteClick = deleteClick;
            this.userControlCommonListHeader1.searchClick = searchClick;
            this.Dock = System.Windows.Forms.DockStyle.Fill;
            //this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            ViewUtils.setDataGridViewStlye(this.dataGridView1);
        }
        //刷新数据
        public void refreshData()
        {

            String search = this.userControlCommonListHeader1.textBox1.Text;
            DataSet ds = dal.GetList("HOUSE_DONG.NAME LIKE '%" + search + "%'  ORDER BY HOUSE_XIAOQU.NAME ASC , HOUSE_DONG.NAME ASC ");
            this.dataGridView1.DataSource = ds.Tables[0];
        }


        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            updateClick();
        }
        private void addClick()
        {
            FormHouseDongUpdate fcv = new FormHouseDongUpdate();
            fcv.oneRefreshParentData = refreshData;
            fcv.ShowDialog(this);
        }
        private void updateClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                int row = this.dataGridView1.CurrentRow.Index;
                int col = this.dataGridView1.CurrentCell.ColumnIndex;
                // String value = this.dataGridView1.SelectedRows[row].Cells["CODE_VALUE"].Value.ToString();
                
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                // this.textBox1.Text = value;
                FormHouseDongUpdate fcv = new FormHouseDongUpdate();



                fcv._ModelHOUSE_DONG = dal.GetModel(Convert.ToInt32(seqid));
                //注册代理
                fcv.oneRefreshParentData = refreshData;
                fcv.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新的记录.", "提示");
            }
        }
        private void deleteClick()
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {


                    if (BllCommonUtil.Exists("SELECT COUNT(1) FROM HOUSE_ROOM WHERE HOUSE_DONG_SEQID = " + seqidInt))
                    {
                        MessageBox.Show("此排层存在房产,不能删除,如需删除,请先删除此排层下的排层房产", "提示");
                        return;

                    }

                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        dal.Delete(seqidInt);
                        //刷新数据
                        refreshData();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }

        }

        private void searchClick()
        {
            String search = "";//this.userControlCommonListSearch1.textBox1.Text;
            refreshData();
        }

        private void UserControlHouseDong_Load(object sender, EventArgs e)
        {
            refreshData();
        }
        //双击更新.
        private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
        {
            updateClick();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }
    }
}
